home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / ZIVFILE.CPP < prev    next >
C/C++ Source or Header  |  1993-08-19  |  10KB  |  355 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    zn.cpp
  5. //   Title:    Zinc Window Template
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains code for the class ZI_VIEW_FILE.
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <zi.hpp>
  41. #define USE_WIN_VIEW_FILE
  42. #if OS_DOS
  43. #include <zid.hpp>
  44. #elif OS_WINDOWS
  45. #include <ziw.hpp>
  46. #else
  47. #include <zio.hpp>
  48. #endif
  49.  
  50.  
  51. //----------------------------------------------------------------------------
  52. //   Description:    Default constructor
  53. //    Parameters:
  54. //       Returns:    
  55. //----------------------------------------------------------------------------
  56. FN_M ZI_VIEW_FILE::ZI_VIEW_FILE(PCSZ _pcszDisplay, PCSZ _pcszPrint, PCSZ _pcszSep)
  57. : ZN_WINDOW("WIN_VIEW_FILE", ZN_LOAD_CENTER|ZN_LOAD_NO_SHOW)
  58. {
  59.     ZI_VIEW_FILE::Initialize(CL_INIT_CLASS);
  60.     Setup();
  61.  
  62.     Assert(_pcszDisplay);
  63.     pcszDisplay = _pcszDisplay;
  64.     pcszPrint    = _pcszPrint;
  65.     pcszSep        = _pcszSep ? _pcszSep: "##";
  66.     Format();
  67. }
  68.  
  69.  
  70. //----------------------------------------------------------------------------
  71. //   Description:    Destructor
  72. //    Parameters:
  73. //       Returns:    
  74. //----------------------------------------------------------------------------
  75. FN_M ZI_VIEW_FILE::~ZI_VIEW_FILE()
  76. {
  77.     ZI_VIEW_FILE::Destroy(FALSE);
  78.     Terminate();
  79. }
  80.  
  81.  
  82. //----------------------------------------------------------------------------
  83. //   Description:    Destroy object. Free any resources used by object.
  84. //                          Normally called by destructor.
  85. //                        Should allow multiple calls from various classes.
  86. //                        A class should almost always re-init its variables when 
  87. //                        it is destroyed to prevent accidents.
  88. //    Parameters:    fDestroyAll        Destroy parents also?
  89. //                                            Default is TRUE.
  90. //       Returns:    TRUE if successful.
  91. //----------------------------------------------------------------------------
  92. BOOL FN_M ZI_VIEW_FILE::Destroy(BOOL fDestroyAll)
  93. {
  94.     ZI_VIEW_FILE::Initialize(CL_INIT_CLASS_VARS);
  95.     if (fDestroyAll)                            // Destroy parent.
  96.         ZI_VIEW_FILE_PARENT::Destroy(fDestroyAll);
  97.     if (pcszTitle)
  98.         pcszTitle = (PCSZ)MemFree((PVOID)pcszTitle);
  99.     return TRUE;
  100. }
  101.  
  102.  
  103. //----------------------------------------------------------------------------
  104. //   Description:    Set field values.
  105. //    Parameters:
  106. //       Returns:    TRUE if valid
  107. //----------------------------------------------------------------------------
  108. BOOL FN_M ZI_VIEW_FILE::Format()
  109. {
  110.     //
  111.     //    Get and verify the full file name
  112.     //
  113.     CHAR szFile[MAX_PATH+1];
  114.     strcpy(szFile, pcszDisplay);
  115.     FnameQualify(szFile, "DOC", EnvGet("PATH"), 0);
  116.  
  117.     if (!FnameIsFile(szFile))
  118.         return Error("File '%s' not found.", szFile);
  119.  
  120.     //
  121.     //    Open file and get size
  122.     //
  123.     BOOL fResult = FALSE;
  124.     FLAG16 fs = FL_OPEN|FL_READWRITE|FL_DENYREADWRITE|FL_BINARY;
  125.     FPOS fsize;
  126.     HF hf = -1;
  127.     PBYTE pbBuf = NULL;
  128.     PCSZ pcszWinTitle = "File not found.";
  129.     PCSZ pcszText = pcszWinTitle;
  130.     PSZ psz;                                        
  131.     PSZ pszCopy;
  132.     BOOL fEol;
  133.     SIZET cSep = strlen(pcszSep);
  134.     SIZET cBuf;
  135.  
  136.     if (!FileOpen(&hf, szFile, fs, NULL))
  137.         goto ERROR_EXIT;
  138.     fsize = FileGetSize(hf);
  139.     if (fsize < 0)
  140.         goto ERROR_EXIT;
  141.     if (fsize == 0 || fsize > 30000L)
  142.         goto ERROR_EXIT;
  143.  
  144.     //
  145.     //    Allocate a buffer and read the file into memory
  146.     //
  147.     pbBuf = (PBYTE)MemAlloc((SIZET)fsize + 1);
  148.     if (pbBuf == NULL)
  149.         {
  150.         ErrorNoMem();
  151.         goto ERROR_EXIT;
  152.         }
  153.     if (!FileRead(hf, pbBuf, (SIZET)fsize, 0))
  154.         goto ERROR_EXIT;
  155.     pbBuf[(SIZET)fsize] = '\0';            // Be sure to add null terminator
  156.     FileClose(hf);
  157.     hf = -1;
  158.                                                     // Remove Ctrl-Z
  159.     if ((psz = strchr((PSZ)pbBuf, '\x1A')) != NULL)
  160.         psz[0] = '\0';
  161.     //
  162.     //    Go through the file removing hard linefeeds and inserting
  163.     //    new one based on the location of separators!!
  164.     //
  165.     Assert(cSep >= 2);
  166.     psz = (PSZ)pbBuf;                            // Eliminate hard line breaks
  167.     pszCopy = (PSZ)pbBuf;
  168.     fEol = FALSE;
  169.     cBuf = strlen(psz);
  170.     while (cBuf)
  171.         {
  172.         if (cBuf >= cSep && memcmp(psz, pcszSep, cSep) == 0)
  173.             {
  174.             cBuf -= cSep;
  175.             psz += cSep;
  176.             *pszCopy++ = '\r';
  177.             *pszCopy++ = '\n';
  178.             fEol = TRUE;
  179.             }
  180.         else if (psz[0] == '\r' || psz[0] == '\n')
  181.             {
  182.             while (psz[0] == '\r' || psz[0] == '\n')
  183.                 {
  184.                 psz++;
  185.                 cBuf--;
  186.                 }
  187.             if (psz[0] && !isspace(psz[0]) && !fEol)
  188.                 *pszCopy++ = ' ';
  189.             fEol = FALSE;
  190.             }
  191.         else if (psz[0] == '\t')
  192.             {
  193.             psz++;
  194.             cBuf--;
  195.             *pszCopy++ = ' ';
  196.             fEol = FALSE;
  197.             }
  198.         else
  199.             {
  200.             *pszCopy++ = *psz++;
  201.             cBuf--;
  202.             fEol = FALSE;
  203.             }
  204.         }
  205.     *pszCopy = '\0';
  206.  
  207.     //
  208.     //    Break out title and get start of text
  209.     //
  210.     pcszWinTitle = (PCSZ)pbBuf;
  211.     psz = strchr(pcszWinTitle, '\r');
  212.     if (psz)                                        // Add null terminator and skip to start of text
  213.         {
  214.         *psz++ = '\0';                    
  215.         while (psz[0] == '\r' || psz[0] == '\n')
  216.             psz++;
  217.         pcszText = (PCSZ)psz;
  218.         }
  219.     else
  220.         pcszText = (PCSZ)pbBuf;
  221.  
  222.     fResult = TRUE;
  223.  
  224.     if (pcszTitle)                                // Set global title
  225.         pcszTitle = (PCSZ)MemFree((PVOID)pcszTitle);
  226.     pcszTitle = MemStrDup(pcszWinTitle);
  227.  
  228. ERROR_EXIT:
  229.     //
  230.     //    Set window and title!!
  231.     //
  232.     SetTitle(pcszTitle);
  233.     SetText(FID(TEXT), pcszText, strlen(pcszText)+1);
  234.  
  235.     //
  236.     //    Clean up!
  237.     //
  238.     if (hf >= 0)
  239.         FileClose(hf);
  240.     if (pbBuf)
  241.         MemFree(pbBuf);
  242.     return fResult;
  243. }
  244.  
  245.  
  246. //----------------------------------------------------------------------------
  247. //   Description:    Initialize object. 
  248. //                          Normally called by constructor.
  249. //                        Should allow multiple calls from various classes.
  250. //    Parameters:    sInit        Initialization code. May be one of the following:
  251. //                                        CL_INIT_CLASS            Reset class variables and
  252. //                                                                    and dynamic allocations for
  253. //                                                                    this class only.
  254. //                                        CL_INIT_CLASS_VARS    Reset class variables for
  255. //                                                                    this class only.
  256. //                                        CL_INIT_VARS            Reset class variables for
  257. //                                                                    this class only.
  258. //                                        CL_INIT_ALL                Initialize class and all 
  259. //                                                                    parent class, including
  260. //                                                                    dynamic memory allocation.
  261. //                                    Default is CL_INIT_ALL
  262. //       Returns:    TRUE if successful.
  263. //----------------------------------------------------------------------------
  264. BOOL FN_M ZI_VIEW_FILE::Initialize(SHORT sInit)
  265. {
  266.     if (sInit == CL_INIT_VARS || sInit == CL_INIT_ALL)
  267.         ZI_VIEW_FILE_PARENT::Initialize(sInit);
  268.  
  269.     pcszDisplay = NULL;
  270.     pcszPrint = NULL;
  271.     pcszSep = NULL;
  272.     pcszTitle = NULL;
  273.     return TRUE;
  274. }
  275.  
  276.  
  277. //----------------------------------------------------------------------------
  278. //   Description:    Set field values.
  279. //    Parameters:
  280. //       Returns:    TRUE if valid
  281. //----------------------------------------------------------------------------
  282. BOOL FN_M ZI_VIEW_FILE::Print()
  283. {
  284.     CHAR szFile[MAX_PATH+1];
  285.  
  286.     if (pcszPrint == NULL || !pcszPrint[0])
  287.         return TRUE;
  288.  
  289.     strcpy(szFile, pcszPrint);                // Qualify file name
  290.     FnameQualify(szFile, "PRN", EnvGet("PATH"), 0);
  291.  
  292.     if (!FnameIsFile(szFile))                // Check if file exists
  293.         return Error("File '%s' not found.", szFile);
  294.  
  295.     return PrintFile(szFile, 0, NULL, pcszTitle);
  296. }
  297.  
  298.  
  299. //----------------------------------------------------------------------------
  300. //   Description:    Event monitor function.
  301. //    Parameters:    msg        Event code
  302. //                        pv1            Data pointer 1
  303. //                        pv2            Data pointer 2
  304. //       Returns:    Event code
  305. //----------------------------------------------------------------------------
  306. ZN_MSG FN_M ZI_VIEW_FILE::User(ZN_MSG msg, PVOID, PVOID)
  307. {
  308.     switch (msg)
  309.         {
  310.         case ZN_MSG_INIT:
  311.             return msg;
  312.  
  313.         case ZN_MSG_TERMINATE:
  314.             return msg;
  315.         }
  316.     if (IsError())                                // Error condition
  317.         return msg;
  318.     switch (msg)
  319.         {
  320.         case TB_CLOSE:
  321.             Close();
  322.             break;
  323.  
  324.         case TB_PRINT:
  325.             Print();
  326.             break;
  327.  
  328.         case TB_HELP:
  329.             break;
  330.         }
  331.     return msg;
  332. }
  333.  
  334.  
  335. //----------------------------------------------------------------------------
  336. //   Description:    
  337. //    Parameters:
  338. //       Returns:    
  339. //----------------------------------------------------------------------------
  340. BOOL FN_M ZI_VIEW_FILE::ViewFile(PCSZ _pcszDisplay, PCSZ _pcszPrint, PCSZ _pcszSep)
  341. {
  342.    PZI_VIEW_FILE pzi_view_file = new ZI_VIEW_FILE(_pcszDisplay, _pcszPrint, _pcszSep);
  343.     if (pzi_view_file == NULL)
  344.         return _ErrorNoMem();
  345.     else if (pzi_view_file->IsValid())
  346.         {
  347.       pzi_view_file->Show();
  348.         return TRUE;
  349.         }
  350.     return FALSE;
  351. }
  352. //----------------------------------------------------------------------------
  353. //------------------------------- End of File --------------------------------
  354. //----------------------------------------------------------------------------
  355.